package ziv.map.activity;

import ziv.map.R;
import ziv.map.utils.Constant;
import ziv.map.utils.DbHelper;
import ziv.map.utils.SqliteDB;

import ziv.utils.html.HttpDownloader;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class OtherPeopleActivity extends Activity {
	 
	SqliteDB sqliteDB = null;
	 
	TextView MOBILEOBSERVE = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		// 数据提取
		sqliteDB = new SqliteDB(this, "observeDB");
		Cursor cursor = sqliteDB.queryData("tb_observe");
		System.out.println("count=" + cursor.getCount());

		setContentView(R.layout.otherpeople);
		MOBILEOBSERVE = (TextView) findViewById(R.id.MOBILEOBSERVE);
		ListView observelistview = (ListView) findViewById(R.id.observelistView);
		// ListView observelistview=getListView();
		SimpleCursorAdapter simpleCursorAdapter = new SimpleCursorAdapter(this,
				R.layout.otherpeople_listviewitem, cursor, new String[] {
						"MOBILEOBSERVE", "RELATION" }, new int[] {
						R.id.MOBILEOBSERVE, R.id.RELATION });
		observelistview.setAdapter(simpleCursorAdapter);

		// setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item,
		// COUNTRIES));
		// ListView lv = getListView();
		// lv.setTextFilterEnabled(true);
		observelistview.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				// When clicked, show a toast with the TextView text
				ListView listView = (ListView) parent;
				Cursor cursor = (Cursor) listView.getItemAtPosition(position);
				Intent intent = new Intent();
				
				
				// 发送get请求连接
				HttpDownloader httpDownloader=new HttpDownloader();
				//Log.v("ziv",cursor.getString(1));
				String response=httpDownloader.download(Constant.memberData+cursor.getString(1));
				
				if(response==null || "".equals(response)){
					
					Toast.makeText(OtherPeopleActivity.this, "没有此号码的位置信息", Toast.LENGTH_LONG);
				}else{
					String [] latLon=response.split(",");
					intent.putExtra("mobile", cursor.getString(1));
					intent.putExtra("lat", latLon[1]);
					intent.putExtra("lon", latLon[0]);
					setResult(RESULT_OK, intent);
					finish();
				}
				
				// Toast.makeText(getApplicationContext(), cursor.getString(1)
				// ,Toast.LENGTH_SHORT).show();
				// cursor.close();
			}
		});
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		sqliteDB.close();
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();

	}

}
